$(function() {
	$.ajaxSetup({
		url : "../data/ajaxSetup.aspx",
		data : {
			"param" : "wyj.wang"
		},
		global : false,
		type : "POST",
		success : function(data, textStatus) {
			$("#divResult").html(data);
		}

	});

	$.post("ShopCartServlet?flag=index", {}, function(data) {

	})
});

function deleteall() {
	$.post("ShopCartServlet?flag=fush", {}, function(obj) {
		if (obj != null) {

			if (obj != null && obj != "") {
				obj = JSON.parse(obj);

				var sids = [];
				for (var i = 0; i < obj.length; i++) {
					if ($("#checkbox" + obj[i].sid).prop("checked") == true) {
						sids.push(obj[i].sid);
					}
				}

				for (let i = 0; i < sids.length; i++) {
					var prices = $('#prices' + sids[i]).text();// 删除选中购物车商品总价
					prices = parseInt(prices);
					var sumspan = $("#sumspan").text();
					sumspan = parseInt(sumspan);
					$('#shop' + sids[i]).remove();
					$("#sumspan").html(sumspan - prices);
				}

				var str = JSON.stringify(sids);
				$.ajax({
					url : "ShopCartServlet?flag=deleteall",
					type : "post",
					dataType : "json",
					data : {
						sids : str
					},
					success : function(data) {

					},
					error : function(data) {

					}

				});
			}

			// $('#sumspan').html(0);${s.clothes.cimgurl }
		}
	});
}

function toOrderpay() {
	$.post("ShopCartServlet?flag=fush", {}, function(obj) {
		if (obj != null) {
			if (obj != null && obj != "") {
				obj = JSON.parse(obj);
				var sids = [];
				for (var i = 0; i < obj.length; i++) {
					if ($("#checkbox" + obj[i].sid).prop("checked") == true) {
						sids.push(obj[i].sid);
						sids[i];
					}
				}
				for (let i = 0; i < sids.length; i++) {
					var prices = $('#prices' + sids[i]).text();// 删除选中购物车商品总价
					prices = parseInt(prices);
					var sumspan = $("#sumspan").text();
					sumspan = parseInt(sumspan);
					$('#shop' + sids[i]).remove();
					$("#sumspan").html(sumspan - prices);
				}

				var str = JSON.stringify(sids);
				$.ajax({
					url : "ShopCartServlet?flag=deleteall",
					type : "post",
					dataType : "json",
					data : {
						sids : str
					},
					success : function(data) {

						location.href = "ToOrderPayServlet?flag=shopcartflag";
					},
					error : function(data) {

					}

				});
			}

			// $('#sumspan').html(0);${s.clothes.cimgurl }
		}
	});
}

function click1(checked, sid) {
	var ss = parseInt($('#sumspan').text());
	var cs = parseInt($('#checkedspan').text());
	var prices = parseInt($('#prices' + sid).text());
	if (checked == true) {
		$("#checkedspan").html(cs + 1);
		$("#sumspan").html(ss + prices);
	}
	if (checked == false) {
		$("#checkedspan").html(cs - 1);
		$("#sumspan").html(ss - prices);
	}
}
function clickall(checked) {
	var sumobj = 0;
	if (checked == true) {
		$.post("ShopCartServlet?flag=sumall", {
			clicked : checked
		}, function(obj) {
			obj = JSON.parse(obj);
			for (var i = 0; i < obj.length; i++) {
				sumobj += obj[i].snum * obj[i].clothes.cprice;
			}
			$("#sumspan").html(sumobj);// 显示总价
			$("#checkedspan").html(obj.length);// 总条数
		});
	} else {
		$("#checkedspan").html(0);
		$("#sumspan").html(0);
	}
}
function deleteshop(flag, sid) {
	if (flag) {
		var prices = $('#prices' + sid).text();// 删除选中购物车商品总价
		prices = parseInt(prices);
		var sumspan = $("#sumspan").text();
		sumspan = parseInt(sumspan);
		$.post("ShopCartServlet?flag=delete", {
			sid : sid,
			prices : prices
		}, function(flag) {
			switch (flag) {
			case "1":
				$('#shop' + sid).remove();
				$("#sumspan").html(sumspan - prices);
				break;
			default:
				break;
			}
		});
	}
}
function shopval(val, price, sid) {// 显示合计 val数量
	var shopnum = "#shopnum" + sid;// 数量标签id
	var pricesname = "#prices" + sid;// 单价标签id
	var price1 = parseInt(price);// 单价
	var pri = parseInt($(shopnum).val());// 数量
	var is = $("#checkbox" + sid).prop("checked");
	var prices = $("#prices" + sid).text();// 单件总价
	var sumspan = parseInt($("#sumspan").text());// 合计
	if (1 <= pri && pri <= 99) {
		prices = pri * price1;
		$(pricesname).html(prices);
		$.post("ShopCartServlet?flag=shopsum", {
			sid : sid,
			val : val
		}, function(obj) {
			if (obj != null && obj != 'x' && obj != 'y') {
				if (is == true) {
					$(pricesname).html(prices);
					sumspan = sumspan + price1 * parseInt(obj);
					$("#sumspan").html(sumspan);
				}
			}
		});
		return;

	} else {
		alert("请输入1-99的数字")
	}
}
function sum1(val, price, sid) {
	var shopnum = "#shopnum" + sid;// 数量标签id
	var pricesname = "#prices" + sid;// 单价标签id
	var price1 = parseInt(price);// 单价
	var pri = parseInt($(shopnum).val());// 数量
	var prices = $("#prices" + sid).text();// 单件总价
	var sumspan = parseInt($("#sumspan").text());// 合计
	var is = $("#checkbox" + sid).prop("checked");
	if (val == '+' && pri == 99) {
		$(pricesname).html(price1 * 99);
		return;
	}
	if (val == '+') {
		pri = pri + 1;
		prices = pri * price1;
		$(pricesname).html(prices);
		if (is == true) {
			sumspan = sumspan + price1;
			$("#sumspan").html(sumspan);
		}
		return;
	}
	if ($(shopnum).val() <= 1) {

		$(pricesname).html(price1);
		return;
	}
	if (val == '-') {
		pri = pri - 1;
		prices = pri * price1;
		$(pricesname).html(prices);
		if (is == true) {
			sumspan = sumspan - price1;
			$("#sumspan").html(sumspan);
		}

	}

	// prices = $(pricesname).text();
	// return $(pricesname).text();

}
function sum(val, price, sid) {
	var shopnum = "#shopnum" + sid;// 数量标签id
	var pricesname = "#prices" + sid;// 单价标签id
	var price1 = parseInt(price);// 单价
	var pri = parseInt($(shopnum).val());// 数量
	var prices = $("#prices" + sid).text();// 单件总价
	var sumspan = parseInt($("#sumspan").text());// 合计
	var is = $("#checkbox" + sid).prop("checked");
	if (val == '+' && pri == 99) {
		$(pricesname).html(price1 * 99);
		return;
	}
	if (val == '+') {
		// pri = pri + 1;
		$.post("ShopCartServlet?flag=sum", {
			snum : pri,
			sid : sid,
			checkedis : is,
			price : price1,
			val : '+'
		}, function(obj) {
			if (obj == '1') {
				prices = pri * price1;
				$(pricesname).html(prices);
				if (is == true) {
					sumspan = sumspan;
					$("#sumspan").html(sumspan);
				}
			}
		});

		return;
	}
	if ($(shopnum).val() <= 1) {
		$(pricesname).html(price1);
		return;
	}
	if (val == '-') {
		// pri = pri - 1;
		$.post("ShopCartServlet?flag=sum", {
			snum : pri,
			sid : sid,
			checkedis : is,
			price : price1,
			val : '-'

		}, function(obj) {
			if (obj == '1') {
				prices = pri * price1;
				$(pricesname).html(prices);
				if (is == true) {
					sumspan = sumspan;
					$("#sumspan").html(sumspan);
				}
			}
		});

		return;
	}

	prices = $(pricesname).text();
	return $(pricesname).text();

}